<?php

class Application_Form_Node extends Zend_Form
{
    protected $_node;
    
    public function __construct($node=null)
    {
        $this->_node=$node;
        
        parent::__construct();
    }
    
    public function init()
    {
        $this->setMethod('POST');
        
        $publish_date=new ZendX_JQuery_Form_Element_DatePicker(
                          'publish_date',
                          array('jQueryParams'=>array(
                                                'dateFormat' => 'dd/mm/yy'
                                                )
                               )
                          );
        $publish_date->setLabel('Publish date')
                     ->addValidator(
                        'Date',
                        true, 
                        array('format'=>'DD/MM/YYYY')
                        )
                     ->addDecorator(new Application_Form_NodeDecorator())
                     ->setValue(!empty($this->_node->publish_date) ? preg_replace('/([\d]{4})-([\d]{2})-([\d]{2})/','$3/$2/$1',$this->_node->publish_date) : date('d/m/Y'))
                     ->setRequired(true);

        $url_alias=new Zend_Form_Element_Text('url_alias');
        $url_alias->setLabel('URL alias')
              ->addFilter('StringTrim')
              ->addFilter('StripTags')
              ->addValidator(
                    'StringLength',
                    true,
                    array(
                        'min' => 3,
                        'max' => 255
                    )
              )
              ->addValidator(
                    new Application_Form_NodeUrlValidator(),
                    true
              )
              ->addDecorator(new Application_Form_NodeDecorator())
              ->setValue(!empty($this->_node->url_alias) ? $this->_node->url_alias: '')
              ->setRequired(false);    

        $language=new Zend_Form_Element_Select('language');
        $language->setLabel('Language')
             ->setMultiOptions(Zend_Registry::get('config')->site->locales->toArray())
             ->addDecorator(new Application_Form_NodeDecorator())
             ->setValue(!empty($this->_node->language) ? $this->_node->language : Zend_Registry::get('config')->site->locale)
             ->setRequired(true);
                       
        $title=new Zend_Form_Element_Text('title');
        $title->setLabel('Title')
              ->addFilter('StringTrim')
              ->addFilter('StripTags')
              ->addValidator(
                    'StringLength',
                    true,
                    array(
                        'min' => 1,
                        'max' => 255
                    )
              )
              ->addDecorator(new Application_Form_NodeDecorator())
              ->setValue(!empty($this->_node->title) ? $this->_node->title : '')
              ->setRequired(true);
        
        $short_text=new Zend_Form_Element_Textarea('short_text');
        $short_text->setLabel('Short text')
              ->addFilter('StringTrim')
              ->addFilter('StripTags')
              ->addValidator(
                    'StringLength',
                    true,
                    array(
                        'max' => 10000
                    )
              )
              ->addDecorator(new Application_Form_NodeDecorator())
              ->setValue(!empty($this->_node->short_text) ? $this->_node->short_text : '')
              ->setRequired(false);
              
        $full_text=new Zend_Form_Element_Textarea('full_text');
        $full_text->setLabel('Full text')
              ->addFilter('StringTrim')
              ->addDecorator(new Application_Form_NodeDecorator())
              ->setValue(!empty($this->_node->full_text) ? $this->_node->full_text : '')
              ->setRequired(false);
        
        $published=new Zend_Form_Element_Checkbox('published');
        $published->setLabel('Publish')
                  ->addDecorator(new Application_Form_NodeDecorator())
                  ->setChecked(!empty($this->_node->published) ? true : false)
                  ->setRequired(false);
                  
        $front=new Zend_Form_Element_Checkbox('front');
        $front->setLabel('Publish on front page')
                  ->addDecorator(new Application_Form_NodeDecorator())
                  ->setChecked(!empty($this->_node->front) ? true : false)
                  ->setRequired(false);

        $selectTypes=array();                 
        $validTypes=Application_Model_NodeTypes::getValidTypes();
        foreach ($validTypes as $validType)
        {
            $selectTypes[$validType]=Application_Model_NodeTypes::getType($validType);
        }
        
        $type=new Zend_Form_Element_Select('type');
        $type->setLabel('Type')
             ->setMultiOptions($selectTypes)
             ->addDecorator(new Application_Form_NodeDecorator())
             ->setValue(!empty($this->_node->type) ? $this->_node->type : '')
             ->setRequired(false);
        
        $selectCategories=array();                 
        $validCategories=Application_Model_NodeCategories::getValidCategories();
        foreach ($validCategories as $validCategory)
        {
            $selectCategories[$validCategory]=Application_Model_NodeCategories::getCategory($validCategory);
        }
             
        $category=new Zend_Form_Element_Select('category');
        $category->setLabel('Category')
             ->setMultiOptions($selectCategories)
             ->addDecorator(new Application_Form_NodeDecorator())
             ->setValue(!empty($this->_node->category) ? $this->_node->category : '')
             ->setRequired(false);
             
        $keywords=new Zend_Form_Element_Textarea('keywords');
        $keywords->setLabel('Keywords')
              ->addFilter('StringTrim')
              ->addValidator(
                    'StringLength',
                    true,
                    array(
                        'max' => 1000
                    )
                )
              ->addDecorator(new Application_Form_NodeDecorator())
              ->setValue(!empty($this->_node->meta['keywords']) ? $this->_node->meta['keywords'] : '')
              ->setRequired(false);
              
        $description=new Zend_Form_Element_Textarea('description');
        $description->setLabel('Meta description')
              ->addFilter('StringTrim')
               ->addValidator(
                    'StringLength',
                    true,
                    array(
                        'max' => 10000
                    )
                )
              ->addDecorator(new Application_Form_NodeDecorator())
              ->setValue(!empty($this->_node->meta['description']) ? $this->_node->meta['description'] : '')
              ->setRequired(false);
              
         $submit=new Zend_Form_Element_Submit('submit');
         $submit->setValue('Save')
                ->setLabel('')
                ->addDecorator(new Application_Form_NodeDecorator());
               
         $csrf=new Zend_Form_Element_Hash('csrf',array('salt' => 'node'));
         $csrf->setOptions(array(
                            'ignore' => true
                        ))
              ->addDecorator(new Application_Form_NodeDecorator());
        
         $this->addElements(array(
            $type,
            $category,
            $publish_date,
            $url_alias,
            $language,
            $title,
            $short_text,
            $full_text,
            $published
         ));   
         
         if (Zend_Registry::get('user')->canPost(new Application_Model_UserResources(Application_Model_UserResources::getFrontResource())))
         {
             $this->addElement($front);
         }
         
         $this->addElements(array(
            $keywords,
            $description,
            $submit,
            $csrf
         ));    
    }
}

